package com.sanyang.pda.ui.manualpicking;

import android.os.Bundle;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.sanyang.pda.bean.ByBarcodeBean;
import com.sanyang.pda.bean.DisassemblyAddInfo;
import com.sanyang.pda.bean.ScanBean;
import com.sanyang.pda.bean.SelectBean;
import com.sanyang.pda.bean.request.DisassemblyAddConfirmRequest;
import com.sanyang.pda.contract.AddPickingContract;
import com.sanyang.pda.dialog.SelectPop;
import com.sanyang.pda.presenter.AddPickingPresenter;
import com.sanyang.pda.utils.StringConfig;
import com.sanyang.pda.utils.Utils;
import com.sanyang.pda.view.ScanButView;
import com.wangshen.base.app.AppActivityKey;
import com.wangshen.base.ui.mvp.base.ui.BaseMvpActivity;
import com.wangshen.projectbase.R;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

@Route(path = AppActivityKey.ADDPICKINGACTIVITY)
public class AddPickingActivity extends BaseMvpActivity<AddPickingPresenter> implements AddPickingContract.View {
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.title)
    FrameLayout title;
    @BindView(R.id.tv_job_location)
    TextView tvJobLocation;
    @BindView(R.id.tv_goods_tray)
    TextView tvGoodsTray;
    @BindView(R.id.tv_goods)
    TextView tvGoods;
    @BindView(R.id.tv_goods_name)
    TextView tvGoodsName;
    @BindView(R.id.tv_good_spec)
    TextView tvGoodSpec;
    @BindView(R.id.tv_batch)
    TextView tvBatch;
    @BindView(R.id.tv_admire_date)
    TextView tvAdmireDate;
    @BindView(R.id.tv_inventory_number)
    TextView tvInventoryNumber;
    @BindView(R.id.tv_choose_number)
    EditText tvChooseNumber;
    @BindView(R.id.comfire)
    Button comfire;
    @BindView(R.id.scan)
    ScanButView scan;
    @BindView(R.id.tv_unit)
    TextView tvUnit;
    private String deliveryNo;
    private String pallet;

    @Override
    public AddPickingPresenter getPresenter() {
        return new AddPickingPresenter();
    }

    @Override
    public int getView() {
        return R.layout.activity_addpicking;
    }

    @Override
    public void initView() {

    }

    @Override
    public void initLinsenterner() {
        scan.setScanCallBack(new ScanButView.ScanCallBack() {
            @Override
            public void callBack(ScanBean scanBean) {
                switch (scanBean.getType()) {
                    case -1://商品
                        //tvGoods.setText(scanBean.getCode());
                        presenter.selectArticleInfoByBarcode(scanBean.getCode(),deliveryNo);
                        break;
                    case 1://作业库位
                        tvJobLocation.setText(scanBean.getCode());
                        break;
                  /*  case 2://作业库位
                        tvJobLocation.setText(scanBean.getCode());
                        break;*/
                    case 4://托盘
                        tvGoodsTray.setText(scanBean.getCode());
                        break;
                }
                if (!TextUtils.isEmpty(tvGoods.getText().toString()) && !TextUtils.isEmpty(tvJobLocation.getText().toString()) && !TextUtils.isEmpty(tvGoodsTray.getText().toString())) {
                    presenter.disassemblyAddInfo(deliveryNo, tvGoodsTray.getText().toString(), tvJobLocation.getText().toString(), myselectBean.getId());
                }
            }
        });
    }
    SelectBean myselectBean;
    List<SelectBean> selectBeanList=new ArrayList<>();
    @Override
    public void setGoodsList(List<ByBarcodeBean> list) {
        if (list.size() == 0){
            showMessage("未查询到商品信息");
            return;
        }
        if (list.size() == 1){
            tvGoods.setText(list.get(0).getBarcode());
            tvGoodsName.setText(list.get(0).getArticleName());
            myselectBean=new SelectBean(list.get(0).getArticleName(),list.get(0).getArticleId());
            myselectBean.setOther(list.get(0).getBarcode());
            if (!TextUtils.isEmpty(tvGoods.getText().toString()) && !TextUtils.isEmpty(tvJobLocation.getText().toString()) && !TextUtils.isEmpty(tvGoodsTray.getText().toString())) {
                presenter.disassemblyAddInfo(deliveryNo, tvGoodsTray.getText().toString(), tvJobLocation.getText().toString(), myselectBean.getId());
            }
        }
        if (list.size() > 1){
            selectBeanList.clear();
            for (int i=0;i< list.size();i++){
                ByBarcodeBean byBarcodeBean=list.get(i);
                SelectBean selectBean=new SelectBean(byBarcodeBean.getArticleName(),byBarcodeBean.getArticleId());
                selectBean.setOther(byBarcodeBean.getBarcode());
                selectBeanList.add(selectBean);
            }
            SelectPop selectPop=new SelectPop(mContext, new SelectPop.Onclick() {
                @Override
                public void onClick(SelectBean content) {
                    myselectBean=content;
                    tvGoods.setText(content.getOther());
                    tvGoodsName.setText(content.getName());
                    if (!TextUtils.isEmpty(tvGoods.getText().toString()) && !TextUtils.isEmpty(tvJobLocation.getText().toString()) && !TextUtils.isEmpty(tvGoodsTray.getText().toString())) {
                        presenter.disassemblyAddInfo(deliveryNo, tvGoodsTray.getText().toString(), tvJobLocation.getText().toString(), myselectBean.getId());
                    }
                }
            });
            selectPop.show(selectBeanList);
        }
    }
    @Override
    public void getData() {
        Bundle bundle = getIntent().getExtras();
        deliveryNo = bundle.getString(StringConfig.DELIVERYNO);
        pallet = bundle.getString(StringConfig.PALLET);
    }

    @OnClick(R.id.comfire)
    public void onViewClicked() {
        if (TextUtils.isEmpty(tvGoods.getText().toString()) || TextUtils.isEmpty(tvJobLocation.getText().toString()) || TextUtils.isEmpty(tvGoodsTray.getText().toString()) || disassemblyAddInfo == null) {
            showMessage(getString(R.string.goods) + getString(R.string.not_be_empty));
            return;
        }
        int num= 0;
        try {
            num = Integer.parseInt(tvChooseNumber.getText().toString());
        } catch (NumberFormatException e) {
            e.printStackTrace();
            showMessage(getString(R.string.input_num));
        }
        if ( num > disassemblyAddInfo.getStockNum() ){
            showMessage(getString(R.string.jxbndykc));
            return;
        }
        DisassemblyAddConfirmRequest request = new DisassemblyAddConfirmRequest();
        request.setArticleId(myselectBean.getId());
        request.setDeliveryNo(deliveryNo);
        request.setPallet(tvGoodsTray.getText().toString());
        request.setPickingPallet(pallet);
        request.setPickedNum(tvChooseNumber.getText().toString() + "");
        request.setStoragebinId(tvJobLocation.getText().toString());
        presenter.disassemblyAddConfirm(request);
    }


    @OnClick(R.id.iv_back)
    public void onViewClicked1() {
        finishActivity();
    }

    private DisassemblyAddInfo disassemblyAddInfo;

    @Override
    public void setData(DisassemblyAddInfo data) {
        this.disassemblyAddInfo = data;
        tvAdmireDate.setText(data.getExpiringTime());
        tvGoodsName.setText(data.getArticleName());
        tvBatch.setText(data.getBatchNo());
        tvGoodSpec.setText(data.getSpec());
        tvUnit.setText(Utils.getArticleUnit(mContext, data.getArticleUnit()));
        tvChooseNumber.setText(data.getPickedNum() + "");
        tvInventoryNumber.setText(data.getStockNum() + "" + Utils.getArticleUnit(mContext, data.getArticleUnit()));
    }

}
